package nl.hanze.designpatterns.gui;


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import nl.hanze.designpatterns.DAO.impl.db.TroubleTicketDAOImpl;
import nl.hanze.designpatterns.domain.TroubleTicket;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class TroubleTicketPanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String username;
	JTextField callernamefield = new JTextField("Enter callername");
	JTextArea descriptionfield = new JTextArea("Enter description");
	public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	
	public TroubleTicketPanel(String username){
		super();
		this.username = username;
		buildPanel();
	}
	
	public void buildPanel(){
		
		Box box = Box.createVerticalBox();
				
		JLabel callername = new JLabel("callername:");		
		JLabel description = new JLabel("descpription");
		JPanel insidepannel = new JPanel(new FlowLayout());		
		JButton addbutton = new JButton("add");
		addbutton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String cname = callernamefield.getText();
				String dfield = descriptionfield.getText();
				Calendar cal = Calendar.getInstance();
				SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
				String date = sdf.format(cal.getTime());
				TroubleTicket tt = new TroubleTicket(username,cname,dfield,date);
				safe(tt);
			}
		});
		JButton clearbutton = new JButton("clear");
		insidepannel.add(addbutton);
		insidepannel.add(clearbutton);
		box.add(callername);
		box.add(callernamefield);
		box.add(description);
		box.add(descriptionfield);
		box.add(insidepannel);
			
		this.add(box);
	}
	
	public void safe(TroubleTicket troubleticket){
		TroubleTicketDAOImpl troubleticketDao = new TroubleTicketDAOImpl();
		troubleticketDao.saveTroubleTicket(troubleticket);
	}
	

}
